class ModeTagLib {

    def selectMode = { attrs ->
        def currentMode = attrs.get('currentMode')
        def projectMode = attrs.get('projectMode')

        String str = "<select id=\"mode\" name=\"mode\">"
            str += "<option"
            if(projectMode==0) str += " selected"
            str += " value=\"0\">Budget only</option>"
            str += "<option"
            if(projectMode==1) str += " selected"
            str += " value=\"1\">Sandbox only</option>"
            str += "<option"
            if(projectMode==2) str += " selected"
            str += " value=\"2\">Budget and Sandbox</option>"
        out << str + "</select>"
    }

    def btnMode = { attrs ->
        def currentMode = attrs.get('mode')
        def currentView = attrs.get('currentView')
        def currentController = attrs.get('currentController')
        def idProject = attrs.get('idProject')
        String str=""

        str += "<div class=\"mode\">"

        // Allow budget mode only between november and december
        Date d = new Date() // get the current date
        d.year=0

        //TODO: get this information from database
       Date startDate = new Date('11/01/1900')
       Date endDate = new Date('12/31/1900 23:59:00')

       // Check if the budget mode is allowed according to the current date
        if((d>=startDate && d<=endDate)){
            str+="<div class=\"switchmodetag\">"
                str+="<form action=\"/MegaSoft/mode/changeMode\" method=\"post\" >"
                if(currentMode != "budget"){ // if in sandbox mode, allow button budget mode
                    str+= "<div class=\"activemodeblock\">"
                    str+= "<div class=\"activemode\">${Inter.printOut('Sandbox mode',session.language)}</div>"
                    str+= "</div>"
                    str += "<input type=\"hidden\" id=\"currentView\" name=\"currentView\" value=\""+currentView+"\"/>"
                    str += "<input type=\"hidden\" id=\"currentController\" name=\"currentController\" value=\""+currentController+"\"/>"
                    str += "<input type=\"hidden\" id=\"idProject\" name=\"idProject\" value=\""+idProject+"\"/>"
                    str += "<span class=\"buttons\">"
                    str += "<input type=\"submit\" class=\"change_mode\" onclick=\"return confirmSave(${session.modified});\" value=\"${Inter.printOut('Switch to budget mode',session.language)}\" name=\"change_mode\"/>"
                    str+= "</span>"
                }
                else{ // else if in budget mode, allow sandbox button
                    str+= "<div class=\"activemode\">${Inter.printOut('Budget mode',session.language)}</div>"
                    str += "<span class=\"buttons\">"
                    str += "<input type=\"hidden\" id=\"currentView\" name=\"currentView\" value=\""+currentView+"\"/>"
                    str += "<input type=\"hidden\" id=\"currentController\" name=\"currentController\" value=\""+currentController+"\"/>"
                    str += "<input type=\"hidden\" id=\"idProject\" name=\"idProject\" value=\""+idProject+"\"/>"
                    str += "<input type=\"submit\" class=\"change_mode\" value=\"${Inter.printOut('Switch to sandbox mode',session.language)}\" name=\"change_mode\"/>"
                    str+= "</span>"
                    
                }
                str += "</form>"
            str += "</div>"
            str += "<div class=\"saveundotag\">"
            if(currentMode != "budget"){ // if in sandbox mode, allow button budget mode
                str += "<span class=\"mode_buttons\">"
                str += "<form id=\"saveModif\" action=\"/MegaSoft/mode/save\" method=\"post\" >"
                str += "<input type=\"hidden\" id=\"currentView\" name=\"currentView\" value=\""+currentView+"\"/>"
                str += "<input type=\"hidden\" id=\"currentController\" name=\"currentController\" value=\""+currentController+"\"/>"
                str += "<input type=\"hidden\" id=\"idProject\" name=\"idProject\" value=\""+idProject+"\"/>"
                str += "<img id=\"saveImg\" src=\"${resource(dir:'images/skin',file:'save.gif')}\" style=\"border:0;margin-right:4px;\" alt=\"Save\" title=\"Save\" />"
                str += "<input type=\"submit\" class=\"change_mode change_mode_saveundo\" value=\"${Inter.printOut('Save',session.language)}\" name=\"save\"/>"
                str += "</form>"
                str+= "</span>"
                str += "<span class=\"mode_buttons\">"
                str += "<form id=\"undoModif\" action=\"/MegaSoft/mode/rollback\" method=\"post\" >"
                str += "<input type=\"hidden\" id=\"currentView\" name=\"currentView\" value=\""+currentView+"\"/>"
                str += "<input type=\"hidden\" id=\"currentController\" name=\"currentController\" value=\""+currentController+"\"/>"
                str += "<input type=\"hidden\" id=\"idProject\" name=\"idProject\" value=\""+idProject+"\"/>"
                str += "<img id=\"undoImg\" src=\"${resource(dir:'images/skin',file:'undo.gif')}\" style=\"border:0;margin-right:4px;\" alt=\"Undo\" title=\"Undo\" />"
                str += "<input type=\"submit\" class=\"change_mode change_mode_saveundo\" onclick=\"return confirm('${Inter.printOut('Sure',session.language)}');\" value=\"${Inter.printOut('Undo',session.language)}\" name=\"undo\"/>"
                str += "</form>"
                str+= "</span>"
            }
            str += "</div>"
            
        }
        else{ // if the current month is before november

            str+= "<span style=\"font-weight:bold; padding-right:10px\">Sandbox mode only</span>"

        }
        //str += currentMode
        str += "</div>"

        out << str
    }

    def btnSaveUndo = { attrs ->
        def currentView = attrs.get('currentView')
        def currentController = attrs.get('currentController')
        def idProject = attrs.get('idProject')
        String str=""
        str += "<span class=\"mode_buttons\">"
        str += "<form id=\"saveModif\" action=\"/MegaSoft/mode/save\" method=\"post\" >"
        str += "<input type=\"hidden\" id=\"currentView\" name=\"currentView\" value=\""+currentView+"\"/>"
        str += "<input type=\"hidden\" id=\"currentController\" name=\"currentController\" value=\""+currentController+"\"/>"
        str += "<input type=\"hidden\" id=\"idProject\" name=\"idProject\" value=\""+idProject+"\"/>"
        str += "<img id=\"saveImg\" src=\"${resource(dir:'images/skin',file:'save.gif')}\" style=\"border:0;margin-right:4px;\" alt=\"Save\" title=\"Save\" />"
        str += "<input type=\"submit\" class=\"change_mode change_mode_saveundo\" value=\"${Inter.printOut('Save',session.language)}\" name=\"save\"/>"
        str += "</form>"
        str+= "</span>"
        str += "<span class=\"mode_buttons\">"
        str += "<form id=\"undoModif\" action=\"/MegaSoft/mode/rollback\" method=\"post\" >"
        str += "<input type=\"hidden\" id=\"currentView\" name=\"currentView\" value=\""+currentView+"\"/>"
        str += "<input type=\"hidden\" id=\"currentController\" name=\"currentController\" value=\""+currentController+"\"/>"
        str += "<input type=\"hidden\" id=\"idProject\" name=\"idProject\" value=\""+idProject+"\"/>"
        str += "<img id=\"undoImg\" src=\"${resource(dir:'images/skin',file:'undo.gif')}\" style=\"border:0;margin-right:4px;\" alt=\"Undo\" title=\"Undo\" />"
        str += "<input type=\"submit\" class=\"change_mode change_mode_saveundo\" onclick=\"return confirm('${Inter.printOut('Sure',session.language)}');\" value=\"${Inter.printOut('Undo',session.language)}\" name=\"undo\"/>"
        str += "</form>"
        str+= "</span>"


        out << str
    }


    def btnLockUnlock = { attrs ->
        def currentView = attrs.get('currentView')
        def currentController = attrs.get('currentController')
        def idProject = attrs.get('idProject')

        String str="<span class=\"mode_buttons\">"
        str += "<form action=\"/MegaSoft/mode/rollback\" method=\"post\" >"
        str += "<input type=\"hidden\" id=\"currentView\" name=\"currentView\" value=\""+currentView+"\"/>"
        str += "<input type=\"hidden\" id=\"currentController\" name=\"currentController\" value=\""+currentController+"\"/>"
        str += "<input type=\"hidden\" id=\"idProject\" name=\"idProject\" value=\""+idProject+"\"/>"
        str += "<input type=\"submit\" class=\"change_mode\" value=\"Unlock\" name=\"undo\"/>"
        str += "</form>"
        str+= "</span>"
        out << str
    }
}
